import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

public class Solution {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CyclicBarrier barrier = new CyclicBarrier(4);
        ExecutorService ex = Executors.newFixedThreadPool(4);
        List<Future<Integer>> futures = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Future<Integer> future = ex.submit(new Task(barrier));
            futures.add(future);
        }
        Integer[] res = new Integer[4];
        for (int i = 0; i < futures.size(); i++) {
            while (!futures.get(i).isDone()) {
                Thread.sleep(1000);
            }
            res[i] = futures.get(i).get();
            ex.shutdown();
        }
        Arrays.sort(res, (o1, o2) -> o2 - o1);
        System.out.println(Arrays.toString(res));
    }

    static class Task implements Callable<Integer> {
        private final CyclicBarrier barrier;

        public Task(CyclicBarrier cyclicBarrier) {
            this.barrier = cyclicBarrier;
        }

        @Override
        public Integer call() {
            try {
                barrier.await();
                int time = (int) (Math.random() * 10 + 10);
                Thread.sleep(time * 1000L);
                System.out.println("Time:\t" + time);
                return time;
            } catch (InterruptedException | BrokenBarrierException ignored) {
            }
            return null;
        }
    }
}
